//
//  ZH_PaymentViewController.m
//  WisdomStar
//
//  Created by zhangliwen on 2018/10/27.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "ZH_PaymentViewController.h"
#import "ZH_PayTableViewCell.h"
#import "ZH_ClassViewModel.h"
#import <AlipaySDK/AlipaySDK.h>
@interface ZH_PaymentViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,weak)UIImageView *bgView;
@property (nonatomic,weak)UILabel *titleLabel;
@property (nonatomic,weak)UILabel *moneyLabel;
@property (nonatomic,weak)UITableView *tableView;
@property (nonatomic,strong)ZH_ClassViewModel *viewModel;
@end

@implementation ZH_PaymentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUserInterface];
}
- (void)cleanUpData{
    [self setViewModel:nil];
}
- (void)initUserInterface{
    self.navigationItem.title = @"选择支付方式";
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.mas_equalTo(FITSCALE(132));
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(FITSCALE(47));
        make.centerX.equalTo(self.view);
    }];
    [self.moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(FITSCALE(9));
        make.centerX.equalTo(self.view);
    }];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView.mas_bottom).offset(FITSCALE(9));
        make.left.right.bottom.equalTo(self.view);
    }];
}
#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        ZH_PayTableViewCell *cell = [ZH_PayTableViewCell cellWithTableView:tableView];
        cell.imageString = @"icon_yepay";
        cell.nameString = @"余额";
        return cell;
    }
    else if(indexPath.row == 1){
        ZH_PayTableViewCell *cell = [ZH_PayTableViewCell cellWithTableView:tableView];
        cell.imageString = @"icon_zfbpay";
        cell.nameString = @"支付宝";
        return cell;
    }
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        [self.viewModel.applyParam setObject:self.sign forKey:@"sign"];
        [self.viewModel.applyParam setObject:self.trade forKey:@"trade_no"];
        WEAKSelf;
        [self.viewModel applyResultBlock:^(NSString *code, NSString *message, id data) {
            if ([code isEqualToString:@"0"]) {
                
                [[AlipaySDK defaultService] payOrder:data[@"data"] fromScheme:@"WisdomStarpay" callback:^(NSDictionary *resultDic) {
                        NSLog(@"reslut = %@",resultDic);
                        if ([resultDic[@"resultStatus"] integerValue] == 9000) {
                            [self.navigationController popViewControllerAnimated:YES];
                            [self.navigationController popViewControllerAnimated:YES];
                       
                    }
                    [weakSelf showHint:@"支付成功"];
                    
                }];
            }
            else{
                [weakSelf showHint:message];
            }
        }];
    }
}
#pragma mark - getter,setter
- (UIImageView *)bgView{
    if (!_bgView) {
        UIImageView *image = [UIImageView createImageViewWithName:@"img_pay_bg"];
        [self.view addSubview:_bgView = image];
    }
    return _bgView;
}
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        UILabel *label = [UILabel createLabelWithText:@"所需支付(元)" font:kFontSize_30];
        label.textColor = [UIColor colorWithHexString:@"#E6E6E6"];
        [self.view addSubview:_titleLabel = label];
    }
    return _titleLabel;
}
- (UILabel *)moneyLabel{
    if (!_moneyLabel) {
        UILabel *label = [UILabel createLabelWithText:@"100元" font:kFontSize_34];
        label.textColor = kFontColorWhite;
        [self.view addSubview:_moneyLabel = label];
    }
    return _moneyLabel;
}
- (UITableView *)tableView{
    if (!_tableView) {
        UITableView *tableView = [UITableView createTableViewWithDelegateTarget:self];
        tableView.backgroundColor = kCommonBackgroudColor;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [tableView setCellAutoAdaptationForEstimatedRowHeight:FITSCALE(100)];
        [self.view addSubview:_tableView = tableView];
    }
    return _tableView;
}
- (ZH_ClassViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[ZH_ClassViewModel alloc] init];
    }
    return _viewModel;
}
@end
